package cn.gov.pbc.address.excel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import cn.gov.pbc.address.pojo.Address;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Getter
@Setter
@Slf4j
public class AddressExcelListener extends AnalysisEventListener<AddressExcelRow> {
	
	private Integer rowIndex = 2;
	
	private List<Address> rowList;
	
	public AddressExcelListener() {
		this.rowList = new ArrayList<>(60000);
	}
	
	

	@Override
	public void invoke(AddressExcelRow data, AnalysisContext context) {
		if(rowIndex % 10000 == 0) {
			System.out.println("正在处理第" + this.rowIndex + "行");
		}
		if(StringUtils.isBlank(data.getCode())) {
			log.info("第{}行数据为空,处理结束", this.rowIndex);
			log.info("共读取{}行数据", this.rowIndex);
			return;
		}
		this.rowList.add(this.assmbleAddress(data));
		this.rowIndex++;
	}
	
	private Address assmbleAddress(AddressExcelRow row) {
		Address address = new Address();
		
		address.setCode(row.getCode());
		address.setName(row.getName());
		address.setProvince(row.getProvince());
		address.setCity(row.getCity());
		address.setDistrict(row.getDistrict());
		address.setAddr(row.getAddr());
		
		return address;
	}
	

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		log.info("共读取{}行数据", this.rowIndex - 2);
	}
}
